﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.Web.Script.Serialization;

namespace AjaxControlFramework
{
	public class CallbackStrategyArguments
	{
		//------// Properties \\--------------------------------------------\\
        private bool? _validationScriptLoaded = null;
        public virtual bool ValidationScriptLoaded
        {
            get
            {
                if (!_validationScriptLoaded.HasValue)
                {
                    return true;
                }
                else
                {
                    return _validationScriptLoaded.Value;
                }
            }
            set { _validationScriptLoaded = value; }
        }


        private string _methodName = null;
        public virtual string MethodName
        {
            get { return _methodName; }
            set
            {
                if (value != null)
                {
                    _methodName = value.Trim();

                    if (_methodName.Length == 0)
                    {
                        _methodName = null;
                    }
                }
            }
        }


        private string _callbackMethod = null;
        public virtual string CallbackMethod
        {
            get { return _callbackMethod; }
            set
            {
                if (value != null)
                {
                    _callbackMethod = value.Trim();

                    if (_callbackMethod.Length == 0)
                    {
                        _callbackMethod = null;
                    }
                }
            }
        }


        private string _controlType = null;
        public virtual string ControlType
        {
            get { return _controlType; }
            set
            {
                if (value != null)
                {
                    _controlType = value.Trim();

                    if (_controlType.Length == 0)
                    {
                        _controlType = null;
                    }
                }
            }
        }


        private bool? _controlTypeExists = null;
        public virtual bool ControlTypeExists
        {
            get
            {
                if (!_controlTypeExists.HasValue)
                {
                    return true;
                }
                else
                {
                    return _controlTypeExists.Value;
                }
            }
            set { _controlTypeExists = value; }
        }


        public int? _shorthandID = null;
        public virtual int ShorthandID
        {
            get
            {
                if (!_shorthandID.HasValue)
                {
                    return 1;
                }
                else
                {
                    return _shorthandID.Value;
                }
            }
            set { _shorthandID = value; }
        }


        private Type _resolvedControlType = null;
        [ScriptIgnore]
        public virtual Type ResolvedControlType
        {
            get
            {
                if (_resolvedControlType == null && !String.IsNullOrEmpty(_controlType))
                {
                    // Check for a virtual path.
                    try
                    {
                        if ((_controlType[0] == '~' || _controlType[0] == '/') && !String.IsNullOrWhiteSpace(System.IO.Path.GetExtension(_controlType)))
                        {
                            _resolvedControlType = BuildManager.GetCompiledType(_controlType);
                        }
                    }
                    catch (ArgumentException)
                    {
                        _resolvedControlType = null;
                    }


                    // If the virtual path check fails, do a lookup of the control type assuming the value of the ControlType argument is a type name.
                    if (_resolvedControlType == null)
                    {
                        _resolvedControlType = AjaxControlUtils.ResolveControlType(_controlType);
                    }
                }

                return _resolvedControlType;
            }
            set { _resolvedControlType = value; }
        }


        private bool? _isTemplateControl = null;
        [ScriptIgnore]
        public virtual bool IsTemplateControl
        {
            get
            {
                if (!_isTemplateControl.HasValue)
                {
                    Type resolvedControlType = ResolvedControlType;

                    if (resolvedControlType != null)
                    {
                        _isTemplateControl = typeof(System.Web.UI.TemplateControl).IsAssignableFrom(ResolvedControlType);
                    }
                    else
                    {
                        _isTemplateControl = false;
                    }
                }

                return _isTemplateControl.Value;
            }
        }


        private bool? _isPage = null;
        [ScriptIgnore]
        public virtual bool IsPage
        {
            get
            {
                if (!_isPage.HasValue)
                {
                    Type resolvedControlType = ResolvedControlType;

                    if (resolvedControlType != null)
                    {
                        _isPage = typeof(System.Web.UI.Page).IsAssignableFrom(ResolvedControlType);
                    }
                    else
                    {
                        _isPage = false;
                    }
                }

                return _isPage.Value;
            }
        }


        private string _outOfBandID = null;
        public virtual string OutOfBandID
        {
            get { return _outOfBandID; }
            set
            {
                if (value != null)
                {
                    _outOfBandID = value.Trim();

                    if (_outOfBandID.Length == 0)
                    {
                        _outOfBandID = null;
                    }
                }
            }
        }


        private bool? _isOutOfBand = null;
        [ScriptIgnore]
        public virtual bool IsOutOfBand
        {
            get
            {
                if (!_isOutOfBand.HasValue)
                {
                    if (!AjaxControlHandler.IsCallback)
                    {
                        _isOutOfBand = false;
                    }
                    else
                    {
                        if (LoadOutOfBand || (CallbackMethod != null && (CallbackMethod == "Load" || CallbackMethod == "GlobalInvoke")))
                        {
                            _isOutOfBand = true;
                        }
                        else
                        {
                            _isOutOfBand = false;
                        }
                    }
                }

                return _isOutOfBand.Value;
            }
        }


        private object[] _parameters = null;
        public virtual object[] Parameters
        {
            get
            {
                if (_parameters == null)
                {
                    _parameters = new object[0];
                }

                return _parameters;
            }
            set { _parameters = value; }
        }


        private Dictionary<string, object> _state = null;
        public virtual Dictionary<string, object> State
        {
            get
            {
                if (_state == null)
                {
                    _state = new Dictionary<string, object>();
                }

                return _state;
            }
            set { _state = value; }
        }
        
        
        public virtual Dictionary<string, object> Properties { get; set; }
        public virtual Dictionary<string, object> LoadProperties { get; set; }

		
        public virtual bool ReturnHtml { get; set; }
        public virtual bool AutoUpdateHtml { get; set; }
        public virtual bool TrackControlState { get; set; }
        public virtual bool LoadOutOfBand { get; set; }
        public virtual bool GZipResponse { get; set; }
        public virtual bool SkipPostBackData { get; set; }
        public virtual string Path { get; set; }
		//------\\ Properties //--------------------------------------------//
	}
}